package request;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * 获取请求头的数据
 */
@WebServlet(name = "request_demo2", value = "/request_demo2")
public class request_demo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //1.获取所有请求头的名称
//        Enumeration<String> headerNames = request.getHeaderNames();//枚举类型
//
//        //2.遍历
//        while (headerNames.hasMoreElements()) {
//            String name = headerNames.nextElement();
//            String value = request.getHeader(name);
//            System.out.println(name+"==>"+value);
//        }

        /**
         * user-agent
         * referer
         */
        //浏览器版本
        System.out.println(request.getHeader("user-agent"));
        //重定向源址
        System.out.println(request.getHeader("referer"));
        String referer = request.getHeader("referer");
        //可以进行防盗链操作
        if(referer!=null && referer.contains("request")){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("成功");

        }
        else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("失败");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
